package com.app.xinfadi.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.app.xinfadi.R;
import com.app.xinfadi.base.BaseActivity;
import com.app.xinfadi.dao.UserDao;
import com.app.xinfadi.util.LogUtil;
import com.app.xinfadi.util.SharedPreferencesUtil;
import com.app.xinfadi.util.Toast;
import com.app.xinfadi.widget.gson.Success;


/**
 * @author WuTengFei <br/>
 * @Function: 更改密码昵称 <br/>
 * @date 2014-11-28 下午7:08:10 <br/>
 */
public class UserModifyPwdActivity extends BaseActivity {

    private EditText et_old_pwd;
    private EditText et_new_pwd;
    private Button btn_submit;
    private EditText et_confirm_pwd;
    private int witch_page;// 0昵称修改 ，1，密码修改
    private View password_ll;
    private View nick_name_rl;
    private EditText et_nick_name;
    private String TAG = UserModifyPwdActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.user_modifypwd);
        initView();
    }

    protected void initView() {
        witch_page = getIntent().getIntExtra("witch_page", -1);


        password_ll = findViewById(R.id.password_ll);
        et_old_pwd = (EditText) findViewById(R.id.et_old_pwd);
        et_new_pwd = (EditText) findViewById(R.id.et_new_pwd);
        et_confirm_pwd = (EditText) findViewById(R.id.et_confirm_pwd);
        btn_submit = (Button) findViewById(R.id.btn_submit);
        btn_submit.setOnClickListener(this);
        et_nick_name = (EditText) findViewById(R.id.et_nick_name);
        nick_name_rl = findViewById(R.id.nick_name_rl);
        if (witch_page == 0) {
            initToolbar("昵称修改");
            nick_name_rl.setVisibility(View.VISIBLE);
        } else if (witch_page == 1) {
            initToolbar("密码修改");
            password_ll.setVisibility(View.VISIBLE);
        }
    }

    private void initToolbar(String title) {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle(title);

        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_submit:
                if (witch_page == 0) {
                    String nick_name = et_nick_name.getText().toString().trim();
                    if (!TextUtils.isEmpty(nick_name)) {
                        // 提交 服务器
                        chageNickName(nick_name);
                    }
                } else if (witch_page == 1) {
                    String old_pwd = et_old_pwd.getText().toString().trim();
                    String new_pwd = et_new_pwd.getText().toString().trim();
                    String confirm_pwd = et_confirm_pwd.getText().toString().trim();
                    if (new_pwd.equals(confirm_pwd)) {
                        // 提交 fuwuqi
                        if (!TextUtils.isEmpty(old_pwd) && !TextUtils.isEmpty(new_pwd)) {
                            // 提交 服务器
                            chagePassword(old_pwd, new_pwd);
                        }

                    } else {
                        Toast.makeText(context, "两次输入不一致", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            default:
                break;
        }
    }

    /**
     * 更改用户名
     *
     * @param name
     */
    private void chageNickName(final String name) {
        UserDao userDao = new UserDao();
        userDao.changeNickName(name, new Response.Listener<Success>() {
            @Override
            public void onResponse(Success success) {
                if (success.success) {
                    Toast.makeText(context, "更改成功", Toast.LENGTH_SHORT).show();
                    Intent intent1 = new Intent();
                    intent1.putExtra("nickname", name);
//                    setResult(Constant.USERDATAACTIVITY_TO_USERMODIFYPWDACTIVITY_NAME, intent1);
                    SharedPreferencesUtil.getInstance().saveUserNickName(name);
                    finish();
                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                LogUtil.e("TAG", error.toString());
            }
        });
    }

    /**
     * 更改密码
     *
     * @param oldpassword
     * @param password
     */
    private void chagePassword(final String oldpassword, final String password) {
        UserDao userDao = new UserDao();
        userDao.changePassword(oldpassword, password, new Response.Listener<Success>() {
            @Override
            public void onResponse(Success success) {
                if (success.success) {
                    SharedPreferencesUtil.getInstance().saveUserPassword(password);
                    LogUtil.e(TAG, "更改成功！");
                    Toast.makeText(context, "更改成功", Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Toast.makeText(context, "更改失败，请检查旧密码是否正确！", Toast.LENGTH_SHORT).show();
                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                LogUtil.e("TAG", error.toString());
            }
        });
    }
}
